package com.woniu.timer;

import com.woniu.adapter.FundAdapter;
import com.woniu.outlet.elasticSearch.FundESDao;
import com.woniu.outlet.elasticSearch.po.Fund;
import com.woniu.outlet.mysql.FundRelaMapper;
import com.woniu.outlet.mysql.po.FundRela;
import com.woniu.outlet.mysql.po.FundRelaExample;
import com.woniu.outlet.redis.FundRedisDao;
import com.woniu.outlet.redis.po.FundRedis;
import com.woniu.util.FundUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.net.URISyntaxException;
import java.util.List;
import java.util.Set;

/**
 * @description:
 * @author: my
 * @time: 2021/4/20 9:54
 */
@Slf4j
@Component
public class FundTimer {

    private FundUtils fundUtils;

    private FundRelaMapper fundRelaMapper;

    private FundRedisDao fundRedisDao;

    private FundESDao fundESDao;

    private FundAdapter fundAdapter;

    private StringRedisTemplate redisTemplate;

    public FundTimer(FundUtils fundUtils,FundRelaMapper fundRelaMapper,
                     FundRedisDao fundRedisDao,FundESDao fundESDao,
                     FundAdapter fundAdapter,StringRedisTemplate redisTemplate){
        this.fundUtils = fundUtils;
        this.fundESDao = fundESDao;
        this.fundRelaMapper = fundRelaMapper;
        this.fundRedisDao = fundRedisDao;
        this.fundAdapter = fundAdapter;
        this.redisTemplate = redisTemplate;
    }

    /**
     * 每天23点更新基金数据
     */
    @Scheduled(cron = "0 0 23 * * ?")
    public void getAllFundTask(){
        List<String> codes = fundAdapter.getCodes("y");
        List<Fund> funds = null;
        try {
            funds = fundUtils.queryFundsByCodes(codes);
            if (funds.size() < 1){
                throw new RuntimeException();
            }
            for (Fund fund : funds) {
                FundRelaExample relaExample = new FundRelaExample();
                relaExample.or().andCodeEqualTo(fund.getCode());
                List<FundRela> fundRelas = fundRelaMapper.selectByExample(relaExample);
                fund.setFundType(fundRelas.get(0).getFundtype());
            }
            fundESDao.deleteAll();
            fundESDao.saveAll(funds);
            log.debug("今日数据等新完成");
        } catch (Exception e) {
            log.debug("定时任务本次获取失败，继续使用昨日数据");
        }
    }

    @Scheduled(cron = "0 0 23 * * ?")
    public void getFundDetail(){
        List<String> codes = fundAdapter.getCodes("y");
        Iterable<FundRedis> all = fundRedisDao.findAll();
        fundRedisDao.deleteAll();
        for (String code : codes) {
            try {
                FundRedis fundRedis = fundUtils.getFundDetail(code);
                fundRedisDao.save(fundRedis);
            } catch (URISyntaxException e) {
                log.debug(code+"基金信息获取失败");
                fundRedisDao.saveAll(all);
                log.debug("数据回滚");
            }
        }
    }
}
